<?php


$base_dir = ".";
$menu_file = "$base_dir/sample.txt";
const MENU = "MENU:";
const SUB_MENU = "SUB-MENU:";
const COMMA = ",";
const COLON = ":";
const CURL_OPEN = "{";
const CURL_CLOSE = "}";
const AT = "@";
const HASH = "#";
const STAR = "*";
const SPL_FIELD_START = "((";
const SPL_FIELD_END = "))";
const HYPHEN = "-";

$mb_var = array();
$mb_menu = array();
$mb_sub_menu = array();
$top_ref = array();
$menu_ref = array();
$r_map = array();
$f_map = array();

$final_sel = "";
$exp_final_sel = "";
$main_menu_selected  = "";
// ==================================================================== //
// MAIN MENU
// ==================================================================== //
if (check_file_exists($menu_file)) {
	parse_menu_file($menu_file);	

	// print & process the collected menu
	create_menu(array_keys($top_ref));
	$sel_menu = read_choice();
	$final_sel .= $sel_menu;
	$main_menu_selected = getSeq($sel_menu);
	create_sub_menu($r_map[$main_menu_selected]);

	//print "FINAL SELECTION: $final_sel\n";
	//print "FINAL EXP SELECTION: \n".print_r($f_map);
	
	foreach ($f_map as $mm => $mr) {
	
		print "\nMain Menu: $mm\n";
		
		foreach ($mr as $sm => $sv) {
			if ( strlen($sm) === 1) {
				print "$sm ==> $sv\n";
			}
		
		}
		
	}
}
// ==================================================================== //


function expand_var($in) {
	global $mb_var;
	$chk1 = strpos($in, CURL_OPEN);
	if ($chk1 !== false) {
		//print "This variable needs to be expanded:: $in\n";
		list($nv, $v) = explode(CURL_OPEN, $in);
		$v = str_replace(CURL_CLOSE, "", $v);
		if (array_key_exists($v, $mb_var)) {
			$in = $mb_var[$v];
			//print "expanded: $in\n";
			expand_var($in);
		} 
	} 
	return $in;
}
function err_exit($mesg) {
	print "$mesg\n";
	exit;
}
function parse_menu_file ($menu_file) {
	global $menu_ref, $r_map, $top_ref, $mb_menu, $mb_sub_menu, $mb_var;
	$fc = file($menu_file);
	
	
	$lc = 0;
	$menuc = 0;
	$submenuc = 0;
	

	while ($lc < count($fc)) {
		// keep iterating the file lines
		$line = trim($fc[$lc]);
		
		// understand menu
		if (startsWith($line, MENU, false)) {
		
			// step1: get the menu, and take all the menu ptrs
			$line = str_replace("MENU:", "", $line);
			$line = trim($line);
			$chk1 = strpos($line, COMMA);
			
			
			if ($chk1 !== false) {
				// multi-menu configuration
				$menuc++;
				$mseq = getSeq($menuc);
				$submenuc=1;
				//print "multi-menu configuration\n";
				$mma = clean_fields($line);
				foreach ($mma as $mmai) {
					array_push($mb_menu, $mmai);
					$top_ref[$mmai] = $mseq;
				}
				
				
			} else {
				// single-menu configuration
				$menuc++;
				$mseq = getSeq($menuc);
				$submenuc=1;
				//print "single-menu configuration\n";
				array_push($mb_menu, $line);
				$top_ref[$line] = $mseq;
				
			}
			
			
			$lc++;
		
		}		
		// understand sub-menu
		else if (startsWith($line, SUB_MENU, false)) {		
			//print "sub-menu\n";
			$menu_deps = "SUB-MENU-";
			$chk3 = strpos($menu_deps, $line);
			if ($chk3 !== false) {
				$lx = str_replace($menu_deps, "", $line);
				list($md, $lx1) = explode(COLON, $line);
				$md = trim(md);
				$mseq = getSeq($menuc);
				$smseq = getSeq($submenuc);
				$r_map[$mseq][$smseq][$md] = $lx1;
			} else {
				$lx = str_replace(SUB_MENU, "", $line);
				$mseq = getSeq($menuc);
				$smseq = getSeq($submenuc);
				$r_map[$mseq][$smseq] = $lx;
			}
			//print "$menuc$submenuc :: $lx\n";
			
			
			$menu_ref["$mseq$smseq"] = $lx;
			$submenuc++;
			$lc++;
		}
		else if ($line === "") {
			//print "empty line\n";
			$lc++;
		}
		// variables
		else {
			//print "var\n";			
			list($var_name, $var_ref) = explode(COLON, $line);
			$var_name = trim($var_name);
			$var_ref = trim($var_ref);
			$mb_var["$var_name"] = $var_ref;
			$lc++;
		}
			
		
	}
	
	/*
	// 12112223432
	// Testing
	print_r($mb_menu);
	print_r($menu_ref);
	print_r($mb_var);
	print_r($top_ref);
	print_r($r_map);
	*/
	
	return $r_map;
	
	
}

	
function clean_fields ($str) {
	$tarr = explode(",", $str);
	
	$rarr = array();
	foreach ($tarr as $titem) {
		array_push($rarr, trim($titem));
	}
	
	return $rarr;
}

function check_file_exists ($filename) {
	if (!file_exists($filename)) {
		//print "Error: $filename not found\n";
		exit;
	}
	return true;
}
function create_sub_menu($inArray) {
	global $final_sel, $exp_final_sel, $main_menu_selected, $f_map;
	$mc=1;
	
	foreach ($inArray as $menu_item) {
		if ($menu_item !== "") {
			//print "===================\n";
			$menu_array = clean_fields($menu_item);
			$exp_menu = array();
			foreach ($menu_array as $mi) {
				$mx = expand_var($mi);
				$tmx = clean_fields($mx);
				$mx = join(COMMA, $tmx);
				$amx = explode(COMMA,$mx);
				$exp_menu = array_merge($exp_menu, $amx);			
			}
				
			create_menu($exp_menu);
			$sel_menu = read_choice();
			$final_sel .= $sel_menu;
			
			$sm_selected = getSeq($mc);
			//print "TESTING: [$main_menu_selected][$sm_selected] ==> $sel_menu\n";
			$f_map[$main_menu_selected][$sm_selected] = $exp_menu[$sel_menu - 1];
			$f_map[$main_menu_selected][$sm_selected.".sel"] = $sel_menu - 1;
			$f_map[$main_menu_selected][$sm_selected.".ref"] = $exp_menu;
			$mc++;
			
		}
	}

}
function create_menu($inArray) {	
	$mc=1;
	foreach ($inArray as $menu_item) {		
		if ($menu_item !== "") {
			print "$mc: $menu_item\n";
			$mc++;
		}
	}	
}

function read_choice() {
	print "\n\nPlease enter your choice no.: ";
	$choice1 = stream_get_line(STDIN, 1024, PHP_EOL);
	print "You selected => $choice1\n";
	return $choice1;
}

function startsWith($haystack,$needle,$case=true) {
    if($case){return (strcmp(substr($haystack, 0, strlen($needle)),$needle)===0);}
    return (strcasecmp(substr($haystack, 0, strlen($needle)),$needle)===0);
}

function endsWith($haystack,$needle,$case=true) {
    if($case){return (strcmp(substr($haystack, strlen($haystack) - strlen($needle)),$needle)===0);}
    return (strcasecmp(substr($haystack, strlen($haystack) - strlen($needle)),$needle)===0);
}

function getSeq($inPtr) {
	
	$seq = array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
	);

	return $seq[$inPtr];

}

function getSeqCS($inPtr) {
	
	$seq = array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
	'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');

	return $seq[$inPtr];

}


/*
$menu_boot_file = "$base_dir/menu.txt";
$menu = file($menu_boot_file);

$mc = 1;
print "Select from the following choices: \n\n";
foreach ($menu as $menu_item) {
	print "$mc: $menu_item";
	$mc++;
}
print "Please enter your choice no.: ";
$choice = stream_get_line(STDIN, 1024, PHP_EOL);
print "You selected => $choice\n";


// Selection1
$s1_file = "$base_dir/$choice1.txt";
if (check_file_exists($s1_file)) {
	$s1 = file($s1_file);
	create_menu($s1);
	$choice = read_choice();
}


// extras
else {
					err_exit("Declaration of $c not found. Please declare.");
				}
*/
?>